﻿﻿﻿require.config({
  paths: {
    'jquery': '/js/packages/jquery-2.14-min',
    'bootstrap': '/bootstrap/js/bootstrap.min',
    'moment':'/bootstrap/bootstrap-date-range/js/moment',
    'daterangepickerzn':'/bootstrap/bootstrap-date-range/js/daterangepicker.zh-CN',
    'bootstrapdaterange': '/bootstrap/bootstrap-date-range/js/daterangepicker'
  },
  shim: {
    'bootstrap': {
      deps: ['jquery'],
      exports: 'bootstrap'
    },
  	'moment':{
  		deps: ['jquery'],
        exports: 'bootstrap'
  	},
  	'bootstrapdaterange':{
  		deps: ['moment'],
        exports: 'bootstrap'
  	},
  	'daterangepickerzn':{
  		deps: ['moment'],
        exports: 'bootstrap'
  	}
  }
  
});
require(['jquery','bootstrap','bootstrapdaterange','moment','daterangepickerzn','bootstrapdaterange'], function ($) {
	var patentName;
	var patentAbstract;
	var claim;
	$('#search-start').click(function(){
		var query = toSearchValue();
		query  = "SELECT * FROM patents WHERE "+query +";";
		alert(query);
		$.ajax({
			url:'/search/similar',
		    type:'POST', //GET
		    async:true,    //或false,是否异步
		    data:{
		    	'query':query
		    },
		    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
		    success:function(data,textStatus,jqXHR){
		    	window.location.href="/patents/similarpatentslist.html";
		    },
		    error:function(xhr,textStatus){
		        console.log('错误')
		        console.log(xhr)
		        console.log(textStatus)
		    },
		    complete:function(){
		        console.log('结束')
		    }
		});
	});
	
	$('#input-patentName').bind('input propertychange', function() {
		patentName=$('#input-patentName').val();
		
		//toSearchValue();
	});
	$('#input-patentAbstract').bind('input propertychange', function() {
		patentAbstract=$('#input-patentAbstract').val();
		//toSearchValue();
	});
	$('#input-claim').bind('input propertychange', function() {
		claim=$('#input-claim').val();
		//toSearchValue();
	});
	function alternative(stringText){
		re = /&/g; 
		result = stringText.replace(re,'"AND"');
		r = /\|/g; 
		result = result.replace(r,'"OR"');
		
		return result;
	}
	function toSearchValue() {
		var search_text="";
		if(!!patentName){
			patentName = alternative(patentName);
			//alert(patentName.split('&'));
			if(!!search_text){
				search_text=search_text+" AND ti = (\""+patentName+"\")"
			}else{
				search_text="ti = (\""+patentName+"\")"
			}
		}
		if(!!patentAbstract){
			patentAbstract = alternative(patentAbstract);
			if(!!search_text){
				search_text=search_text+" AND ab = (\""+patentAbstract+"\")"
			}else{
				search_text="ab = (\""+patentAbstract+"\")"
			}
		}
		if(!!claim){
			claim = alternative(claim);
			if(!!search_text){
				search_text=search_text+" AND cl = (\""+claim+"\")"
			}else{
				search_text="cl = (\""+claim+"\")"
			}
		}
		return search_text;
	}
});
